using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SqlPrompt
{
	public class Config
	{
		public List<DbConnectionInfo> DbConnections = new List<DbConnectionInfo>();

		public int GetConnectionIndexFromName(string name)
		{
			if (DbConnections == null || DbConnections.Count == 0)
				return -1;

			for (int i = 0; i < DbConnections.Count; i++)
				if (DbConnections[i].Name.ToLower() == name.ToLower())
					return i;

			return -1;
		}

		public static Config Load()
		{
			if (File.Exists(ConfigPath))
				return (Config)SerializationUtil.DeserializeFromXmlFile(ConfigPath, typeof(Config));

			return null;
		}

		public void Save()
		{
			SerializationUtil.SerializeToXmlFile(this, ConfigPath);
		}

		public static string WorkingDirectory
		{
			get
			{
				return Path.GetDirectoryName(Application.ExecutablePath);
			}
		}

		public static string ConfigPath
		{
			get
			{
				return Path.Combine(WorkingDirectory, "config.xml");
			}
		}
	}
}
